package org.chen.utils;

import org.chen.mapper.AvatarExcludeMapper;
import org.chen.response.AvatarResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Component
public class AvatarServiceUtils {

    @Autowired
    private AvatarExcludeMapper excludeMapper;

    public List<AvatarResponse.Avatar> getCombinedAvatars(AvatarResponse response) {
        List<String> excludeNames = excludeMapper.selectAllExcludeNames();
        List<AvatarResponse.Avatar> avatars = response.getData().getAvatars();

        // 使用实际的排除名单过滤
        Predicate<AvatarResponse.Avatar> excludeFilter =
                avatar -> excludeNames.stream().noneMatch(name -> avatar.getAvatar_name().contains(name));

        // 获取 Female 性别的前 10 条，排除指定的名字
        List<AvatarResponse.Avatar> topFemaleAvatars = avatars.stream()
                .filter(avatar -> avatar.getGender().equalsIgnoreCase("Female"))
                .filter(excludeFilter) // 排除指定的名字
                .limit(20)
                .collect(Collectors.toList());

        // 获取 Male 性别的前 10 条，排除指定的名字
        List<AvatarResponse.Avatar> topMaleAvatars = avatars.stream()
                .filter(avatar -> avatar.getGender().equalsIgnoreCase("Male"))
                .filter(excludeFilter) // 排除指定的名字
                .limit(20)
                .toList();

        // 获取 Premium 头像（高级版）的前 10 条，排除指定的名字
        List<AvatarResponse.Avatar> topPremiumAvatars = avatars.stream()
                .filter(avatar -> avatar.getPremium() != null && avatar.getPremium())
                .filter(excludeFilter) // 排除指定的名字
                .limit(20)
                .toList();

        // 获取 Basic 头像（基础版）的前 10 条，排除指定的名字
        List<AvatarResponse.Avatar> topBasicAvatars = avatars.stream()
                .filter(avatar -> avatar.getPremium() != null && !avatar.getPremium())
                .filter(excludeFilter) // 排除指定的名字
                .limit(20)
                .toList();

        // 合并所有列表，去重处理（如果有重复的话）
        topFemaleAvatars.addAll(topMaleAvatars);
        topFemaleAvatars.addAll(topPremiumAvatars);
        topFemaleAvatars.addAll(topBasicAvatars);

        // 去重处理（如果头像有重复）
        return topFemaleAvatars.stream()
                .distinct()
                .collect(Collectors.toList());
    }
}